//
//  ImageDeleteView.m
//  PianoAndPoemDirectSeeding
//
//  Created by 梅津山 on 2017/7/19.
//  Copyright © 2017年 梅津山. All rights reserved.
//

#import "ImageDeleteView.h"

@interface ImageDeleteView ()

@property (nonatomic, strong) UIView *homeV;


@end

@implementation ImageDeleteView

#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height

+ (instancetype)sharedRect:(CGRect)rect{
    static ImageDeleteView *imageV;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        imageV = [[ImageDeleteView alloc] initWithFrame:rect];
    });
    return imageV;
}

//点击图片后的方法(即图片的放大全屏效果)
- (void) tapActionImage:(UIImage *)image{
    //创建一个黑色背景
    //初始化一个用来当做背景的View。我这里为了省时间计算，宽高直接用的5s的尺寸
    [self setBackgroundColor:[UIColor blackColor]];
    //创建显示图像的视图
    //初始化要显示的图片内容的imageView（这里位置继续偷懒...没有计算）
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, SCREEN_W - 40, 400)];
    //要显示的图片，即要放大的图片
    imgView.image = image;
    [self addSubview:imgView];
    
    imgView.userInteractionEnabled = YES;
    //添加点击手势（即点击图片后退出全屏）
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];
    [imgView addGestureRecognizer:tapGesture];
    
    //删除按钮
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(SCREEN_W / 2 - 20, 550, 40, 40);
    [deleteBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(deleteBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];

    //放大过程中出现的缓慢动画
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.2;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [self.layer addAnimation:animation forKey:nil];
}
//删除按钮点击事件
- (void)deleteBtnClicked{
    [self closeView];
    self.deleteBlock();
}
//图片点击事件
-(void)closeView{
    [self removeFromSuperview];
    self.navigBlock();
}

@end
